/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package spynx.desl.impl;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;

import org.eclipse.emf.ecore.impl.ENotificationImpl;

import spynx.desl.DeslPackage;
import spynx.desl.Expression;
import spynx.desl.IfStatement;
import spynx.desl.Statement;
import spynx.desl.runtime.IExecutionContext;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>If Statement</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link spynx.desl.impl.IfStatementImpl#getCondition <em>Condition</em>}</li>
 *   <li>{@link spynx.desl.impl.IfStatementImpl#getTrueStatement <em>True Statement</em>}</li>
 *   <li>{@link spynx.desl.impl.IfStatementImpl#getFalseStatement <em>False Statement</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class IfStatementImpl extends StatementImpl implements IfStatement {
	/**
	 * The cached value of the '{@link #getCondition() <em>Condition</em>}' containment reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getCondition()
	 * @generated
	 * @ordered
	 */
	protected Expression condition;

	/**
	 * The cached value of the '{@link #getTrueStatement() <em>True Statement</em>}' containment reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getTrueStatement()
	 * @generated
	 * @ordered
	 */
	protected Statement trueStatement;

	/**
	 * The cached value of the '{@link #getFalseStatement() <em>False Statement</em>}' containment reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getFalseStatement()
	 * @generated
	 * @ordered
	 */
	protected Statement falseStatement;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected IfStatementImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return DeslPackage.Literals.IF_STATEMENT;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Expression getCondition() {
		return condition;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetCondition(Expression newCondition, NotificationChain msgs) {
		Expression oldCondition = condition;
		condition = newCondition;
		if (eNotificationRequired()) {
			ENotificationImpl notification = new ENotificationImpl(this, Notification.SET, DeslPackage.IF_STATEMENT__CONDITION, oldCondition, newCondition);
			if (msgs == null) msgs = notification; else msgs.add(notification);
		}
		return msgs;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setCondition(Expression newCondition) {
		if (newCondition != condition) {
			NotificationChain msgs = null;
			if (condition != null)
				msgs = ((InternalEObject)condition).eInverseRemove(this, EOPPOSITE_FEATURE_BASE - DeslPackage.IF_STATEMENT__CONDITION, null, msgs);
			if (newCondition != null)
				msgs = ((InternalEObject)newCondition).eInverseAdd(this, EOPPOSITE_FEATURE_BASE - DeslPackage.IF_STATEMENT__CONDITION, null, msgs);
			msgs = basicSetCondition(newCondition, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, DeslPackage.IF_STATEMENT__CONDITION, newCondition, newCondition));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Statement getTrueStatement() {
		return trueStatement;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetTrueStatement(Statement newTrueStatement, NotificationChain msgs) {
		Statement oldTrueStatement = trueStatement;
		trueStatement = newTrueStatement;
		if (eNotificationRequired()) {
			ENotificationImpl notification = new ENotificationImpl(this, Notification.SET, DeslPackage.IF_STATEMENT__TRUE_STATEMENT, oldTrueStatement, newTrueStatement);
			if (msgs == null) msgs = notification; else msgs.add(notification);
		}
		return msgs;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setTrueStatement(Statement newTrueStatement) {
		if (newTrueStatement != trueStatement) {
			NotificationChain msgs = null;
			if (trueStatement != null)
				msgs = ((InternalEObject)trueStatement).eInverseRemove(this, EOPPOSITE_FEATURE_BASE - DeslPackage.IF_STATEMENT__TRUE_STATEMENT, null, msgs);
			if (newTrueStatement != null)
				msgs = ((InternalEObject)newTrueStatement).eInverseAdd(this, EOPPOSITE_FEATURE_BASE - DeslPackage.IF_STATEMENT__TRUE_STATEMENT, null, msgs);
			msgs = basicSetTrueStatement(newTrueStatement, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, DeslPackage.IF_STATEMENT__TRUE_STATEMENT, newTrueStatement, newTrueStatement));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Statement getFalseStatement() {
		return falseStatement;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetFalseStatement(Statement newFalseStatement, NotificationChain msgs) {
		Statement oldFalseStatement = falseStatement;
		falseStatement = newFalseStatement;
		if (eNotificationRequired()) {
			ENotificationImpl notification = new ENotificationImpl(this, Notification.SET, DeslPackage.IF_STATEMENT__FALSE_STATEMENT, oldFalseStatement, newFalseStatement);
			if (msgs == null) msgs = notification; else msgs.add(notification);
		}
		return msgs;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setFalseStatement(Statement newFalseStatement) {
		if (newFalseStatement != falseStatement) {
			NotificationChain msgs = null;
			if (falseStatement != null)
				msgs = ((InternalEObject)falseStatement).eInverseRemove(this, EOPPOSITE_FEATURE_BASE - DeslPackage.IF_STATEMENT__FALSE_STATEMENT, null, msgs);
			if (newFalseStatement != null)
				msgs = ((InternalEObject)newFalseStatement).eInverseAdd(this, EOPPOSITE_FEATURE_BASE - DeslPackage.IF_STATEMENT__FALSE_STATEMENT, null, msgs);
			msgs = basicSetFalseStatement(newFalseStatement, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, DeslPackage.IF_STATEMENT__FALSE_STATEMENT, newFalseStatement, newFalseStatement));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case DeslPackage.IF_STATEMENT__CONDITION:
				return basicSetCondition(null, msgs);
			case DeslPackage.IF_STATEMENT__TRUE_STATEMENT:
				return basicSetTrueStatement(null, msgs);
			case DeslPackage.IF_STATEMENT__FALSE_STATEMENT:
				return basicSetFalseStatement(null, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case DeslPackage.IF_STATEMENT__CONDITION:
				return getCondition();
			case DeslPackage.IF_STATEMENT__TRUE_STATEMENT:
				return getTrueStatement();
			case DeslPackage.IF_STATEMENT__FALSE_STATEMENT:
				return getFalseStatement();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case DeslPackage.IF_STATEMENT__CONDITION:
				setCondition((Expression)newValue);
				return;
			case DeslPackage.IF_STATEMENT__TRUE_STATEMENT:
				setTrueStatement((Statement)newValue);
				return;
			case DeslPackage.IF_STATEMENT__FALSE_STATEMENT:
				setFalseStatement((Statement)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case DeslPackage.IF_STATEMENT__CONDITION:
				setCondition((Expression)null);
				return;
			case DeslPackage.IF_STATEMENT__TRUE_STATEMENT:
				setTrueStatement((Statement)null);
				return;
			case DeslPackage.IF_STATEMENT__FALSE_STATEMENT:
				setFalseStatement((Statement)null);
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case DeslPackage.IF_STATEMENT__CONDITION:
				return condition != null;
			case DeslPackage.IF_STATEMENT__TRUE_STATEMENT:
				return trueStatement != null;
			case DeslPackage.IF_STATEMENT__FALSE_STATEMENT:
				return falseStatement != null;
		}
		return super.eIsSet(featureID);
	}

	@Override
	public Object execute(IExecutionContext context) {
		boolean condition = (Boolean) getCondition().execute(context);
		if (condition)
			return getTrueStatement().execute(context);
		else if (getFalseStatement() != null)
			return getFalseStatement().execute(context);
		return null;
	}
} //IfStatementImpl
